package com.rivulet.foreign.service.impl;

import com.rivulet.foreign.mapper.MessageMapper;
import com.rivulet.foreign.pojo.Message;
import com.rivulet.foreign.service.MessageService;
import com.rivulet.foreign.tools.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * {一句话形容此class功能}
 *
 * @author xujiac
 * @create 2021/6/3
 * @since 1.0.0
 */
@Service
public class MessageServiceImpl implements MessageService {
    final
    MessageMapper messageMapper;

    public MessageServiceImpl(MessageMapper messageMapper) {
        this.messageMapper = messageMapper;
    }

    @Override
    public Results getAllMessage() {
        Results results = new Results(100, null);
        try {
            List<Message> list = messageMapper.getAllMessage();
            results.setData(list);
        } catch (Exception e) {
            results.setCode(404);
        }
        return results;
    }

    @Override
    public Results setMessage(Message message) {
        Results results = new Results(404, null);
        try {
            messageMapper.setMessage(message.getUser_id(), message.getContent(), message.getUptime());
            results.setCode(100);
            results.setMsg("发送成功");
        } catch (Exception e) {
            results.setMsg("留言异常，请重试");
        }
        return results;
    }
}
